﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace MyFinanceManager.Objects
{
    public struct CatInfo
    {
        public string Text { get; set; }
        public string Tag { get; set; }
        public int CatValue { get; set; }
        public string Name
        {
            get
            {
                if (!String.IsNullOrEmpty(this.Tag))
                    return this.Tag;
                return this.Text;
            }
        }
    }

    public static class Helper
    {
        public static List<CatInfo> Load(string filename)
        {
            List<CatInfo> catInfos = new List<CatInfo>();
            using (StreamReader sr = File.OpenText(filename))
            {
                while (!sr.EndOfStream)
                {
                    string s = sr.ReadLine();
                    if (s.Contains(";"))
                    {
                        string[] splitted = s.Split(new char[] { ';' });

                        CatInfo cInfo = new CatInfo();

                        try
                        {
                            if (splitted.Length == 3)
                                cInfo = new CatInfo { CatValue = int.Parse(splitted[0]), Text = splitted[1], Tag = splitted[2] };
                            else
                                cInfo = new CatInfo { CatValue = int.Parse(splitted[0]), Text = splitted[1] };

                            catInfos.Add(cInfo);
                        }
                        catch
                        {
                            //TODO Exceptionhandling
                            Console.WriteLine("Parsing Error!");
                        }
                    }
                }
            }
            return catInfos;
        }

        public static List<CatInfo> GetSubCatInfos(int catId, IEnumerable<CatInfo> list)
        {
            return (from cInfo in list
                    where cInfo.CatValue == catId
                    select cInfo).ToList<CatInfo>();
        }

        public static int GetCountFromCategory(int catId, IEnumerable<CatInfo> list)
        {
            return (from cInfo in list
                    where cInfo.CatValue == catId
                    select cInfo).Count<CatInfo>();
        }

        public static List<PostingRecord> GetRecordsFromAccount(string accountName, IEnumerable<PostingRecord> list)
        {
            return (from item in list
                    where item.AccountName == accountName
                    select item).ToList<PostingRecord>();
        }

        public static List<string> GetSubCatNames(int catId, string filename)
        {
            List<CatInfo> catInfos = Helper.Load(filename);
            return (from cInfo in catInfos
                    where cInfo.CatValue == catId
                    select cInfo.Name).ToList<string>();
        }
    }
}
